import VueRouter from "vue-router";

import School from "../components/School/School";
import TodoLists from "../components/todoLists/TodoLists";
import Detail from "../components/School/Detail";
import Info from "../components/School/Info";
const router = new VueRouter({
	mode:"history",
	routes:[
		{
			path:'/school',
			component:School,
			meta:{isAuth:true,title:'学校'},
			children:[
				{
					name:"detail",
					path:'detail',
					component:Detail,
					// props:{a:1,b:2},   第一种写法: 对象写法,固定值
					//props:true,         第二种写法: 把params参数,以props的形式传递给组件
					// 第三种写法
					props($route){
						return {id:$route.query.id,title:$route.query.title}
					}
				},
				{
					name:"info",
					path:'info/:id/:title',
					component:Info,
					meta:{isAuth:true}
				}
			]
		},
		{
			path:'/todolist',
			component:TodoLists,
			meta:{title: "待办"},
			beforeEnter(to,from,next){
				console.log({to,from,next})
				next();
			}
		}
	]
})
// 全局前置路由守卫
// 初始化和每次路由切换调用
router.beforeEach((to, from, next)=>{
	if (to.meta.isAuth){
		if (localStorage.getItem("name")==='yushi'){
			next();
		}else {
			alert("进不了");
		}
	}else {
		next();
	}
})
// 后置路由守卫 --初始化的时候调用  每次路由切换之后调用
router.afterEach((to)=>{
	document.title=to.meta.title || '系统'
})
export default router;
